package o;

import android.content.pm.PackageInfo;
import android.content.pm.ProviderInfo;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MatrixCursor.RowBuilder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.annotation.NonNull;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.microsearch.bean.MicroSearchAppInfo;
import com.huawei.appgallery.microsearch.bean.MicroSearchInfoResBean;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.serverreqkit.api.listener.IServerCallBack;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;

public final class cmr {
    private static final String[] ˋ = new String[]{"suggest_text_1", "suggest_text_2", "suggest_text_3", "suggest_app_status", "suggest_content_type", "suggest_icon_data", "suggest_shortcut_id", "in_progress", "suggest_intent_data_id"};
    private static final Object ˎ = new Object();
    private static volatile cmr ॱ = null;
    private Handler ʻ;
    private final Map<String, cmj> ʽ;
    private String ˊ;
    private boolean ˏ;
    private final Map<String, List<cmm>> ॱॱ;

    private cmr() {
        this.ˊ = "";
        this.ˏ = false;
        this.ॱॱ = new HashMap();
        this.ʽ = new HashMap();
        this.ʻ = new Handler(this, Looper.getMainLooper()) {
            final /* synthetic */ cmr ॱ;

            public void handleMessage(Message message) {
                cmh.ˎ.ˋ("MicroSearchManager", "keyWordHandler handleMessage :" + message.what);
                this.ॱ.ˋ(((Bundle) message.obj).getString("limit"));
            }
        };
        this.ˏ = ˏ();
    }

    public static cmr ॱ() {
        if (ॱ == null) {
            synchronized (ˎ) {
                if (ॱ == null) {
                    ॱ = new cmr();
                }
            }
        }
        return ॱ;
    }

    public MatrixCursor ˋ(String str, String str2) {
        if (this.ॱॱ.size() >= 5) {
            ˎ();
        }
        List list = (List) this.ॱॱ.get(str);
        this.ˊ = str;
        this.ʻ.removeMessages(16);
        if (!cgs.ˋ(list)) {
            return ˋ(str, list);
        }
        Message obtainMessage = this.ʻ.obtainMessage();
        Bundle bundle = new Bundle();
        bundle.putString("limit", str2);
        obtainMessage.obj = bundle;
        obtainMessage.what = 16;
        this.ʻ.sendMessageDelayed(obtainMessage, 500);
        return null;
    }

    public void ˊ(String str, int i) {
        if (this.ˏ) {
            ˏ(1, str, i, true);
        } else {
            cmh.ˎ.ˋ("MicroSearchManager", "No register, notifyChangeInstallProvider:do not provider Micro Search.");
        }
    }

    public void ˎ(String str, int i) {
        if (this.ˏ) {
            ˏ(0, str, i, true);
        } else {
            cmh.ˎ.ˋ("MicroSearchManager", "No register,notifyChangeDownloadProvider:do not provider Micro Search.");
        }
    }

    public void ˊ(String str) {
        if (this.ˏ) {
            ˏ(0, str, -1, false);
            ˏ(1, str, 0, true);
            return;
        }
        cmh.ˎ.ˋ("MicroSearchManager", "No register,notifyChangeUnInstallProvider:do not provider Micro Search.");
    }

    public void ˋ() {
        this.ˊ = "";
        cmh.ˎ.ˋ("MicroSearchManager", "query searchKeyWord is null , will cleanSearchResultOfInstalled");
        List<String> arrayList = new ArrayList();
        for (Entry entry : this.ॱॱ.entrySet()) {
            List<cmm> list = (List) entry.getValue();
            String str = (String) entry.getKey();
            for (cmm o_cmm : list) {
                if (cep.ˋ(cme.ˋ(), o_cmm.ʽ())) {
                    arrayList.add(str);
                    this.ʽ.remove(o_cmm.ʽ());
                    break;
                }
            }
        }
        if (!cgs.ˋ(arrayList)) {
            for (String str2 : arrayList) {
                this.ॱॱ.remove(str2);
            }
        }
    }

    public Cursor ॱ(@NonNull Uri uri, String[] strArr, String str, String[] strArr2, String str2) {
        String str3;
        String queryParameter;
        cmr ॱ;
        Cursor cursor = null;
        if (strArr2 != null) {
            try {
                if (strArr2.length > 0) {
                    str3 = strArr2[0];
                    if (str3.length() <= 100) {
                        cmh.ˎ.ˊ("MicroSearchManager", "searchKeyWord length is too large");
                    } else {
                        queryParameter = uri.getQueryParameter("limit");
                        cmh.ˎ.ˋ("MicroSearchManager", "query(****," + queryParameter + ") start");
                        ॱ = ॱ();
                        if (ॱ == null) {
                            cmh.ˎ.ˊ("MicroSearchManager", "query dataCreater is null!");
                        } else if (cgy.ॱ(str3)) {
                            if (cgy.ॱ(queryParameter)) {
                                queryParameter = "3";
                            }
                            if (cdh.ˎ()) {
                                cmh.ˎ.ˊ("MicroSearchManager", "query(...) interrupted , protocol is not agree. ");
                            } else {
                                cursor = ॱ.ˋ(str3, queryParameter);
                            }
                        } else {
                            ॱ.ˋ();
                        }
                    }
                    return cursor;
                }
            } catch (UnsupportedOperationException e) {
                cmh.ˎ.ˊ("MicroSearchManager", "query  UnsupportedOperationException:" + e.toString());
            }
        }
        str3 = "";
        if (str3.length() <= 100) {
            queryParameter = uri.getQueryParameter("limit");
            cmh.ˎ.ˋ("MicroSearchManager", "query(****," + queryParameter + ") start");
            ॱ = ॱ();
            if (ॱ == null) {
                cmh.ˎ.ˊ("MicroSearchManager", "query dataCreater is null!");
            } else if (cgy.ॱ(str3)) {
                if (cgy.ॱ(queryParameter)) {
                    queryParameter = "3";
                }
                if (cdh.ˎ()) {
                    cursor = ॱ.ˋ(str3, queryParameter);
                } else {
                    cmh.ˎ.ˊ("MicroSearchManager", "query(...) interrupted , protocol is not agree. ");
                }
            } else {
                ॱ.ˋ();
            }
        } else {
            cmh.ˎ.ˊ("MicroSearchManager", "searchKeyWord length is too large");
        }
        return cursor;
    }

    private void ˎ() {
        cmh.ˎ.ˋ("MicroSearchManager", "query cache limit arrived , will cleanSearchResult");
        this.ॱॱ.clear();
        this.ʽ.clear();
        this.ˊ = "";
    }

    private void ˋ(String str) {
        if (cgy.ॱ(this.ˊ)) {
            cmh.ˎ.ˋ("MicroSearchManager", "searchDataFromCloud , lastedKeyWord is null ");
            return;
        }
        cfw.ˊ(cml.APIMETHOD, MicroSearchInfoResBean.class);
        cvt o_cml = new cml("4026632");
        try {
            o_cml.ˋ(Integer.parseInt(str));
        } catch (NumberFormatException e) {
            cmh.ˎ.ˊ("MicroSearchManager", "NumberFormatException:setMaxResults_" + e.getMessage());
        }
        o_cml.ˋ(this.ˊ);
        o_cml.ॱ(0);
        o_cml.ˏ(1);
        if (cme.ˋ().getResources().getConfiguration().locale != null) {
            o_cml.setLocale_(cdn.ˊ());
        }
        cfw.ˊ(o_cml, new IServerCallBack(this) {
            final /* synthetic */ cmr ˋ;

            {
                this.ˋ = r1;
            }

            public void prePostResult(BaseRequest o_cvu, ResponseBean responseBean) {
            }

            public void notifyResult(BaseRequest o_cvu, ResponseBean responseBean) {
                cml o_cml = (cml) o_cvu;
                if (!cgy.ॱ(o_cml.ˋ()) && !cgy.ॱ(this.ˋ.ˊ) && o_cml.ˋ().equals(this.ˋ.ˊ) && responseBean != null && responseBean.getResponseCode() == 0 && (responseBean instanceof MicroSearchInfoResBean)) {
                    List ˏ = this.ˋ.ॱ(o_cml.ˋ(), ((MicroSearchInfoResBean) responseBean).ˋ());
                    this.ˋ.ॱॱ.put(this.ˋ.ˊ, ˏ);
                    if (!cgs.ˋ(ˏ) && this.ˋ.ˏ(o_cml.ˋ())) {
                        this.ˋ.ˊ();
                    }
                }
            }
        });
    }

    private List<cmm> ॱ(String str, List<MicroSearchAppInfo> list) {
        if (cgs.ˋ(list) || cgy.ॱ(str)) {
            return null;
        }
        List<cmm> arrayList = new ArrayList(list.size());
        for (MicroSearchAppInfo microSearchAppInfo : list) {
            if (microSearchAppInfo.ˏ() == null || !microSearchAppInfo.ˏ().toLowerCase(Locale.US).contains(str.toLowerCase(Locale.US))) {
                cmh.ˎ.ˋ("MicroSearchManager", "convertSearchInfo2Result continue, package:" + microSearchAppInfo.ʽ() + ",name:" + microSearchAppInfo.ˏ() + ",but not contain :" + str);
            } else {
                cmm o_cmm = new cmm();
                o_cmm.ˋ(microSearchAppInfo.ˏ());
                o_cmm.ॱ(microSearchAppInfo.ॱॱ() + "\b" + microSearchAppInfo.ˊ());
                o_cmm.ˎ(microSearchAppInfo.ʻ());
                cmj o_cmj = (cmj) this.ʽ.get(microSearchAppInfo.ʽ());
                if (o_cmj != null) {
                    o_cmm.ˎ(new cmj(o_cmj.ˎ, o_cmj.ॱ));
                } else {
                    o_cmm.ˎ(new cmj(String.valueOf(-1), String.valueOf(0)));
                }
                o_cmm.ˏ(microSearchAppInfo.ˋ());
                o_cmm.ʻ(microSearchAppInfo.ʽ());
                o_cmm.ॱॱ(microSearchAppInfo.ˏॱ());
                o_cmm.ˊ(microSearchAppInfo.ॱˎ());
                o_cmm.ˊ(microSearchAppInfo.ॱ());
                o_cmm.ᐝ(microSearchAppInfo.ͺ());
                o_cmm.ʼ(microSearchAppInfo.ˊॱ());
                arrayList.add(o_cmm);
            }
        }
        return arrayList;
    }

    private MatrixCursor ˋ(String str, List<cmm> list) {
        if (cgs.ˋ(list) || cgy.ॱ(str)) {
            return null;
        }
        cmh.ˎ.ˋ("MicroSearchManager", "query local results : " + list.size());
        MatrixCursor matrixCursor = new MatrixCursor(ˋ, 1);
        int size = list.size();
        for (int i = 0; i < size; i++) {
            cmm o_cmm = (cmm) list.get(i);
            if (o_cmm.ˋ() == null || !o_cmm.ˋ().toLowerCase(Locale.US).contains(str.toLowerCase(Locale.US))) {
                cmh.ˎ.ˋ("MicroSearchManager", "query result continue, package:" + o_cmm.ʽ() + ",name:" + o_cmm.ˋ() + ",but not contain :" + str);
            } else {
                RowBuilder newRow = matrixCursor.newRow();
                cmh.ˎ.ˋ("MicroSearchManager", "query result package:" + o_cmm.ʽ() + ",name:" + o_cmm.ˋ() + ",status=" + o_cmm.ˊ());
                newRow.add(o_cmm.ˋ()).add(o_cmm.ˏ()).add(o_cmm.ॱ()).add(o_cmm.ˊ().ˎ).add(o_cmm.ˊ().ॱ).add(ˎ(str, o_cmm.ˎ())).add(o_cmm.ʽ()).add(ˎ(o_cmm.ʽ())).add(o_cmm.ʻ());
            }
        }
        return matrixCursor;
    }

    private String ˎ(String str) {
        String str2 = "0";
        if (cgy.ॱ(str)) {
            return str2;
        }
        SessionDownloadTask ˏ = cmn.ˎ().ˏ(str);
        if (ˏ != null) {
            return String.valueOf(ˏ.ʼॱ());
        }
        return str2;
    }

    private byte[] ˎ(String str, String str2) {
        byte[] bArr = new byte[0];
        if (!cgy.ॱ(str2)) {
            bArr = cms.ॱ(cea.ˏ(str2));
            if (bArr.length > 0) {
                cmh.ˎ.ˋ("MicroSearchManager", " getAppIcon from local finished ");
            } else {
                ˊ(str, str2);
            }
        }
        return bArr;
    }

    private void ˊ(String str, String str2) {
        try {
            cdx.ˊ(new 1(this, str2, str));
        } catch (Exception e) {
            cmh.ˎ.ˊ("MicroSearchManager", "refreshImageFromServer error:" + e.toString());
        }
    }

    private synchronized void ˏ(int i, String str, int i2, boolean z) {
        if (str == null) {
            cmh.ˎ.ˊ("MicroSearchManager", "notifyChangeProvider error: packageName is null");
        } else {
            Object obj = null;
            StringBuilder stringBuilder = new StringBuilder(HiAnalyticsConstant.REPORT_VAL_SEPARATOR);
            for (Entry entry : this.ॱॱ.entrySet()) {
                stringBuilder.append((String) entry.getKey()).append(HiAnalyticsConstant.REPORT_VAL_SEPARATOR);
                List<cmm> list = (List) entry.getValue();
                if (!cgs.ˋ(list)) {
                    Object obj2;
                    for (cmm o_cmm : list) {
                        if (str.equals(o_cmm.ʽ())) {
                            cmh.ˎ.ˋ("MicroSearchManager", "notifyChangeProvider package : " + o_cmm.ʽ() + "," + o_cmm.ˋ() + ",type:" + i + ",status=" + i2 + ",isNotify=" + z);
                            if (i == 0) {
                                o_cmm.ˊ().ˎ = String.valueOf(i2);
                                ॱ(o_cmm.ʽ(), String.valueOf(i2), null);
                            } else if (i == 1) {
                                o_cmm.ˊ().ॱ = String.valueOf(i2);
                                ॱ(o_cmm.ʽ(), null, String.valueOf(i2));
                            }
                            obj2 = 1;
                            obj = obj2;
                        }
                    }
                    obj2 = obj;
                    obj = obj2;
                }
            }
            if (z && obj != null) {
                if (ॱ(stringBuilder.toString())) {
                    ˊ();
                }
            }
        }
    }

    private void ॱ(String str, String str2, String str3) {
        cmj o_cmj = (cmj) this.ʽ.get(str);
        if (o_cmj == null) {
            o_cmj = new cmj(String.valueOf(-1), String.valueOf(0));
        }
        if (!cgy.ॱ(str2)) {
            o_cmj.ˎ = str2;
        }
        if (!cgy.ॱ(str3)) {
            o_cmj.ॱ = str3;
        }
        this.ʽ.put(str, o_cmj);
    }

    private boolean ˏ() {
        try {
            PackageInfo packageInfo = cme.ˋ().getPackageManager().getPackageInfo(cme.ˋ().getPackageName(), 8);
            if (!(packageInfo == null || packageInfo.providers == null || packageInfo.providers.length <= 0)) {
                for (ProviderInfo providerInfo : packageInfo.providers) {
                    if ("com.huawei.appmarket.micro.searchmanager.CONTENT_URI".equals(providerInfo.authority)) {
                        cmh.ˎ.ˋ("MicroSearchManager", "isRegisterMicroSearchProvider:true");
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            cmh.ˎ.ˊ("MicroSearchManager", "can not find app:" + e.toString());
        }
        cmh.ˎ.ˋ("MicroSearchManager", "isRegisterMicroSearchProvider:false");
        return false;
    }

    private boolean ॱ(String str) {
        return (cgy.ॱ(str) || cgy.ॱ(this.ˊ) || !str.contains(HiAnalyticsConstant.REPORT_VAL_SEPARATOR + this.ˊ + HiAnalyticsConstant.REPORT_VAL_SEPARATOR)) ? false : true;
    }

    private boolean ˏ(String str) {
        return (cgy.ॱ(str) || cgy.ॱ(this.ˊ) || !str.equals(this.ˊ)) ? false : true;
    }

    private void ˊ() {
        cme.ˋ().getContentResolver().notifyChange(Uri.parse("content://com.huawei.appmarket.micro.searchmanager.CONTENT_URI/"), null);
    }
}
